<?php

/**
 * This is the model class for table "team".
 *
 * The followings are the available columns in table 'team':
 * @property string $id
 * @property string $user_id
 * @property string $title
 * @property string $summary
 * @property string $city_id
 * @property string $partner_id
 * @property double $team_price
 * @property double $market_price
 * @property string $product
 * @property string $per_number
 * @property string $min_number
 * @property string $max_number
 * @property string $now_number
 * @property string $image
 * @property string $mobile
 * @property string $fare
 * @property string $address
 * @property string $detail
 * @property string $notice
 * @property string $express
 * @property string $delivery
 * @property string $state
 * @property string $buyonce
 * @property string $expire_time
 * @property string $begin_time
 * @property string $end_time
 * @property string $reach_time
 * @property string $close_time
 *
 * The followings are the available model relations:
 */
class Team extends CActiveRecord
{
	 public $image;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Team the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'team';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(


			array('team_price, market_price', 'numerical'),
			array('title','required'),
			array('user_id, city_id, partner_id, per_number, min_number, max_number, now_number, fare, expire_time, begin_time,  reach_time, close_time', 'length', 'max'=>10),
			array('end_time','length','max'=>16),
			array('title, product, image, address', 'length', 'max'=>128),
			array('mobile', 'length', 'max'=>16),
			array('delivery, state', 'length', 'max'=>7),
			array('buyonce', 'length', 'max'=>1),
			array('summary, detail, notice, express', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, title, summary, city_id, partner_id, team_price, market_price, product, per_number, min_number, max_number, now_number, image, mobile, fare, address, detail, notice, express, delivery, state, buyonce, expire_time, begin_time, end_time, reach_time, close_time', 'safe', 'on'=>'search'),
			//array('image', 'file', 'types'=>'jpg, gif, png'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'city'=>array(self::BELONGS_TO,'Category','city_id'),
			'partner'=>array(self::BELONGS_TO,'Partner','partner_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => '用户ID',
			'title' => '项目名称',
			'summary' => '项目描述',
			'city_id' => '城市',
			'partner_id' => '商家',
			'team_price' => '团购价格',
			'market_price' => '市场价格',
			'product' => '商品名称',
			'per_number' => '每人限购',
			'min_number' => '成团最低数量',
			'max_number' => '团购最高数量',
			'now_number' => '目前购买数量',
			'image' => '商品图片',
			'mobile' => '联系电话',
			'fare' => '快递费用',
			'address' => '商家地址',
			'detail' => '本单详情',
			'notice' => '特别提示',
			'express' => '配送说明',
			'delivery' => '配送方式',
			'state' => '状态',
			'buyonce' => '仅购买一次',
			'expire_time' => '优惠券有效期',
			'begin_time' => '开始时间',
			'end_time' => '结束时间',
			'reach_time' => '成团时间',
			'close_time' => '关闭时间',
		);
	}
	protected function beforeSave()
{
    if(parent::beforeSave())
    {

            $this->begin_time= time();
            $this->end_time=strtotime($this->end_time);
            $this->expire_time=strtotime($this->expire_time);
            return true;
    }
    else return false;
}


	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('summary',$this->summary,true);
		$criteria->compare('city_id',$this->city_id,true);
		$criteria->compare('partner_id',$this->partner_id,true);
		$criteria->compare('team_price',$this->team_price);
		$criteria->compare('market_price',$this->market_price);
		$criteria->compare('product',$this->product,true);
		$criteria->compare('per_number',$this->per_number,true);
		$criteria->compare('min_number',$this->min_number,true);
		$criteria->compare('max_number',$this->max_number,true);
		$criteria->compare('now_number',$this->now_number,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('fare',$this->fare,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('detail',$this->detail,true);
		$criteria->compare('notice',$this->notice,true);
		$criteria->compare('express',$this->express,true);
		$criteria->compare('delivery',$this->delivery,true);
		$criteria->compare('state',$this->state,true);
		$criteria->compare('buyonce',$this->buyonce,true);
		$criteria->compare('expire_time',$this->expire_time,true);
		$criteria->compare('begin_time',$this->begin_time,true);
		$criteria->compare('end_time',$this->end_time,true);
		$criteria->compare('reach_time',$this->reach_time,true);
		$criteria->compare('close_time',$this->close_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}